package com.xyj.zjbz.interceptor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.xyj.zjbz.po.Role;
import com.xyj.zjbz.po.User;
import com.xyj.zjbz.user.dao.UserDAO;

public class UserAuthorities implements UserDetailsService {
	@Autowired
	UserDAO userDAO;

	/**
	 * 覆写此方法，
	 */
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		User u = userDAO.getUserByLoginName(username);
		Role role = userDAO.getRoleById(u.getRoleId());// 这里只设置了一个用户对应一个角色
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
		UserDetails ud = new org.springframework.security.core.userdetails.User(
				u.getLoginName(), u.getPwd(), true, true, true, true,
				authorities);
		return ud;
	}

}
